package com.jinghong.epidemiccontrolproject.controller;

import com.jinghong.epidemiccontrolproject.mapper.PatientMapper;
import com.jinghong.epidemiccontrolproject.mapper.RegionMapper;
import com.jinghong.epidemiccontrolproject.pojo.Patient;
import com.jinghong.epidemiccontrolproject.pojo.Region;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:ExcelController
 * Package:com.jinghong.epidemiccontrolproject.controller
 * Description:
 *
 * @Author: 寻月君子
 * @CreatTime:2024/2/2 20:04
 * Version:1.0
 */
@RestController
public class ExcelController {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RegionMapper regionMapper;

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response,
                            String name, Short gender, Integer age1, Integer age2, Integer regionId, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end, Short status) throws Exception {

        //创建工作簿对象
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();

        //创建表格，指定工作簿名称
        XSSFSheet sheet = xssfWorkbook.createSheet("患者表格");


        //XSSFRow row = sheet.createRow(0);//括号中的数值就是行的索引数值,本文采用遍历的方式创建工作簿这一行代码不需要



        // 创建表头
        XSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("姓名");
        header.createCell(1).setCellValue("性别");
        header.createCell(2).setCellValue("年龄");
        header.createCell(3).setCellValue("身份证号码");
        header.createCell(4).setCellValue("地区");
        header.createCell(5).setCellValue("地址");
        header.createCell(6).setCellValue("确诊时间");
        header.createCell(7).setCellValue("更新时间");
        header.createCell(8).setCellValue("状态");


        // 填充数据
        List<Patient> patients = patientMapper.list(name, gender, age1, age2, regionId, begin, end, status);
        int rowIndex = 1;
        for (Patient patient : patients) {
            XSSFRow row = sheet.createRow(rowIndex++);
            row.createCell(0).setCellValue(patient.getName());
            row.createCell(1).setCellValue(patient.getGender()== 1?'男': '女');
            row.createCell(2).setCellValue(patient.getAge());
            row.createCell(3).setCellValue(patient.getIdCardNum());
            //拿到地区
            Region region = regionMapper.getById(patient.getRegionId());
            row.createCell(4).setCellValue(region.getName());
            row.createCell(5).setCellValue(patient.getAddress());
            row.createCell(6).setCellValue(String.valueOf(patient.getCertifyTime()));
            row.createCell(7).setCellValue(String.valueOf(patient.getUpdateTime()));
            if (patient.getStatus()==1){
                row.createCell(8).setCellValue("感染");
            } else if (patient.getStatus()==2) {
                row.createCell(8).setCellValue("康复");
            }else {
                row.createCell(8).setCellValue("死亡");
            }

        }


        //设置表格的名称，通过时间设置名称
        String fileName = "patientExport"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss"))+".xlsx";
        //创建文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\"+fileName);

        xssfWorkbook.write(fileOutputStream);


        // ServletOutputStream outputStream = response.getOutputStream();




        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Transfer-Encoding", "binary");

        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");

        response.setHeader("Content-Disposition", "attachment;filename=\"" +fileName+ ".xlsx\"");
        xssfWorkbook.write(response.getOutputStream());

        xssfWorkbook.close();
    }


}


